package com.fzzz.kotlindemo

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView
import org.jetbrains.anko.find

class MainActivity : AppCompatActivity() {
    private var tvContent: TextView? = null;
    private var btn: Button? = null;
    private lateinit var btn_anko: Button;

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        btn = findViewById(R.id.btn)
        btn = find(R.id.btn)
//        tvContent = findViewById(R.id.tv_content)
        tvContent = find(R.id.tv_content)
        btn_anko = find(R.id.btn_anko)

        btn?.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            var user = User("jack", 12)
            intent.putExtra("user", user)
            startActivityForResult(intent, 1)
        }

        btn_anko.setOnClickListener {
            val intent = Intent(this, AnkoActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                val user = data?.getParcelableExtra<User>("user")
                tvContent?.setText(user?.name + "----" + user?.age)
            }
        }
    }
}
